﻿///<jscompress sourcefile="core.js" />
/**
 *  Magic core 1.2
 *  Author: Mac_J@boarsoft.com
 */
var mac = { Msg: Msg || {} };
if (jQuery)(function() {
	$.extend($.fn, {
		mac : function() {
			var func = arguments[0];
			arguments[0] = this;
			try {
				return eval('mac.' + func).apply(this, arguments);
			} catch (e) {
				alert(func + ': ' +  e);
			}
		},
		seek : function(name) {
			return $(this).find('[name=' + name + ']');
		}
	});
})(jQuery);
mac.getMousePos = function(e){
	var e = e || window.event, d = document
		, de = d.documentElement, db = d.body;
	return {
		x: e.pageX || (e.clientX + (de.scrollLeft || db.scrollLeft)), 
		y: e.pageY || (e.clientY + (de.scrollTop || db.scrollTop))
	}
}
mac.eval = function(str) {
	if (typeof(str) == 'string') {
		return str ? eval('(' + str + ')') : {};
	}
	return str;
};
mac.msg = function(m, p) {
	if (p && p.length) {
		for (var i = 0; i < p.length; i++) {
			var s = p[i] + '';
			if (Msg.status && s.length > 4 && !s.indexOf('Msg.')) {
				s = eval(s);
			}
			m = m.replace('{' + i + '}', s);
		}
	}
	return m;
};;
///<jscompress sourcefile="combo.js" />
/**
 * Magic ComboBox v2.1.4
 * Author: Mac_J@boarsoft.com
 * Need: core.js
 * 
 * 1、支持数组参数
 * 2、增加disable方法
 * 3、增加val(?)方法
 */
mac.combo = function(self, cfg) {
	self.config = cfg = $.extend({
		height : 24,
		maxkLen : 20,
		comma : ',',
		name : self.prop('name')
	}, cfg);
	self.selected = cfg.selected || [];
	var cols = cfg.cols;
	var cw = cfg.width, ch = cfg.height;
	var hd = $('<div class="head"><div>').width(cw).height(ch);
	var tf = $('<input type="text" />').height(ch).width('100%');
	var tw = $('<div class="left"></div>').css('margin-right', ch).height(ch + 2);
	tf.prop({
		name : cfg.name,
		maxLength : cfg.maxLen
	}).click(function(){
		return false;
	}).keydown(function(e) {
		var k = e.keyCode, op = self.isOpen
			, rs = bd.find('.tr'), v = tf.val();
		if (e.ctrlKey && k == 'a') {
			return true;
		}
		switch (k) {
		case 9: //TAB
		case 27:// ESC
			self.close();
			tf.focus();
			break;
		case 38: // UP
		case 40: // DOWN
			var d = (k == 40), h = rs.filter('.hover')
				, s = rs.filter('.selected'), x;
			if (!op && d) {
				self.key = v;
				self.open();
			}
			if (h.length) {
				h = h.removeClass('hover').first();
				x = d? h.next():h.prev();
			}
			if (!x || !x.length) {
				x = d? rs.first() : rs.last();
			}
			if (x.length) {
				x.addClass('hover');
			}
			return false;
		case 13:// RETURN
			var h = rs.filter('.hover');
			if (op) {
				if (h.length) {
					h.click();
					h.removeClass('hover');
				}
			} else {
				self.open();
			}
			return false;
		}
		return true;
	});
	var btn = self.btn = $('<div class="btn"></div>');
	var ico = $('<span class="icon icon-triangle-1-s"></span>');
	ico.css('margin', Math.floor(ch / 2 - 8)).appendTo(btn);
	btn.height(ch).width(ch).css('margin-top', -ch - 2);
	tw.append(tf);
	hd.append(tw).append(btn);
	var bd = $('<div class="body"></div>');
	if (cfg.boxHeight)
		bd.height(cfg.boxHeight);
	if (cfg.boxEl) {
		bd.append(cfg.boxEl);
		bd.addClass('box');
	}
	bd.click(function(e) {
		e.stopPropagation();
		return false;
	});
	btn.click(function() {
		if (self.hasClass('disabled')) {
			return;
		}
		if (self.isOpen) {
			self.close();
		} else {
			self.open();
		}
		return false;
	});
	self.append(hd).append(bd);
	if (cfg.readOnly) {
		tf.attr('readonly', 'readonly');
		tf.click(function() {
			btn.click();
			return false;
		});
	}
	self.close = function() {
		self.isOpen = false;
		bd.children('.tr').removeClass('hover');
		bd.hide();
		if (cfg.onClose) {
			cfg.onClose(self);
		}
	}
	tf.change(function() {
		if (!tf.val()) {
			self.select([]);
		}
	});
	self.open = function() {
		self.isOpen = true;
		var w = cfg.boxWidth || self.width()
			, p = self.position(), bt = cfg.boxTop, bl = cfg.boxLeft;
		bt = (bt || bt == 0) ? bt : (cfg.height + 2);
		bl = (bl || bl == 0) ? bl : 0;
		bd.css({
			top : p.top + bt,
			left : p.left + bl
		});
		bd.width(Math.max(w, cfg.boxMinWidth || w)).show();
		if (cfg.onOpen) {
			cfg.onOpen(self);
		}
		$(document).one('click', self.close);
	}
	self.enable = function(){
		self.removeClass('disabled');
	}
	self.disable = function(){
		self.addClass('disabled');
	}
	self.val = function(dd, dv){
		if (arguments.length) {
			self.select(dd, dv);
		} else {
			var s = self.selected, r;
			if (s && s.pop) {
				r = cfg.multiSelect? s:s[0];
			}
			if (cfg.allowInput && !r) {
				r = tf.val();
			}
			return r;
		}
	}
	self.txt = function(v){
		delete self.selected;
		if (arguments.length) {
			tf.val(v);
		} else {
			return tf.val();
		}
	}
	self.checkin = function(dd){
		if (dd != undefined) {
			if (!$.isArray(dd)) {
				dd = (typeof dd == 'string') ? dd.split(cfg.comma) : [ dd ];
			}
			if (!cfg.multiSelect && dd.length > 1) {
				dd = [ dd[0] ];
			}
		} else {
			dd = [];
		}
		return dd;
	}
	self.select = function(dd, tt) {
		if (cfg.allowInput && (typeof dd == 'string') && !self.data[dd]) {
			tf.val(dd);
			return;
		}
		dd = self.checkin(dd);
		if (tt != undefined) {
			self.selected = dd;
			tf.val(tt);
			return;
		}
		var dv = [], sd = [], sk = [], kf = cfg.keyField
			, df = cfg.displayField, kd = (kf && df);
		if (cfg.boxEl) {
			$.each(dd, function(n, d) {
				sk.push(kf ? d[kf] : d[n])
				dv.push(kd ? d[df] : d[n]);
				sd.push(d);
			});
		} else {
			dd = $.map(dd, function(c) {
				return '' + c;
			});
			bd.find('.tr').each(function(n, c) {
				var a = $(c), k = a.attr('name')
					, d = self.data[k], sc = 'selected';
				a.removeClass(sc).show();
				if (dd.length > 0) {
					if ($.inArray(k, dd) > -1) {
						dv.push(kd ? d[df] : d);
						sk.push(k);
						sd.push(d);
						a.addClass(sc);
					}
				}
			});
		}
		self.selected = sk;
		dv = self.displayValue = dv.join(cfg.comma);
		tf.val(dv);
		if (cfg.onSelect) {
			cfg.onSelect(self, sk, sd);
		}
//		if (!cfg.multiSelect && ) {
//			self.close();
//		}
		return self;
	}
	self.loadData = function(dd) {
		if (cfg.boxEl) {
			cfg.boxEl.loadData(dd);
			return;
		}
		bd.empty();
		if (!dd || !dd.length)
			return;
		var kf = cfg.keyField, df = cfg.displayField, kd = (kf && df);
		self.data = (kd ? {}:[]);
		var mc = cols && cols.length;
		$.each(dd, function(i, r) {
			var k = kd ? r[kf] : i;
			var a = $('<div name="' + k + '" class="tr"></div>');
			self.data[k] = r;
			if (kf && mc) {
				$.each(cols, function(n, c) {
					if (c.hidden)
						return;
					var b = $('<div class="td"></div>');
					b.width(c.width);
					b.append(c.render ? c.render(r, a, self) : r[c.field]);
					a.append(b);
				});
			} else {
				var b = $('<div class="td"></div>');
				b.append(kd? r[df] : r).width('100%');
				a.append(b);
			}
			var hc = 'hover';
			a.click(function() {
				var sl = self.selected || [];
				var i = $.inArray(k, sl), b = i > -1;
				if (cfg.multiSelect) {
					b ? sl.splice(i, 1) : sl.push(k);
					self.select(sl);
					return false;
				} else {
					self.select([k]);
					self.close();
				}
			}).hover(function() {
				bd.children('.tr').removeClass(hc);
				$(this).addClass(hc);
			}, function() {
				$(this).removeClass(hc);
			});
			bd.append(a);
		});
		if (cfg.onLoad) {
			cfg.onLoad(self, dd);
		}
	}
	var ldr = cfg.loader;
	if (cfg.data) {
		self.loadData(cfg.data);
	} else if (ldr && ldr.url) {
		$.post(ldr.url, ldr.params, function(data) {
			var ro = mac.eval(data);
			if (ro.success) {
				ro = ro.data;
				self.loadData(ro.list);
			}
		});
	}
	return self;
};
///<jscompress sourcefile="combo_autoCp.js" />
/**
 * Magic ComboBox AutoComplete Plugin v1.0
 * author: Mac_J@boarsoft.com
 * need combo.js, core.js
 */
mac.combo_autoCp = function(self, cfg) {
	cfg = $.extend(self.config, {
		autoCp : $.extend({}, cfg)
	});
	var acfg = cfg.autoCp;
	var sc = 'selected', hc = 'hover'
	var tf = self.children('.head').find('input');
	var bd = self.children('.body');
	var fb = acfg.by || cfg.keyField;
	tf.keyup(function(e) {
		var k = e.keyCode, op = self.isOpen;
		var rs = bd.children('.tr'), v = ':visible';
		switch (k) {
		case 38: // UP
		case 40: // DOWN
			var x, d = (k == 40);
			if (!op && d)
				self.open();
			rs = rs.filter(v);
			var h = rs.filter('.' + hc), s = rs.filter('.' + sc);
			rs.removeClass(hc);
			var hx = (d ? h.nextAll(v) : h.prevAll(v)).first();
			var sx = (d ? s.nextAll(v) : s.prevAll(v)).first();
			if (h.length + s.length == 0 || hx.length + sx.length == 0) {
				x = (d ? rs.first() : rs.last());
			} else if (hx.length) {
				x = hx;
			} else if (sx.length && op && !h.length) {
				x = sx;
			} else if (s.length && !op) {
				x = s;
			} else {
				x = x = (d ? rs.first() : rs.last());
			}
			x.addClass(hc);
			return false;
		case 13:// RETURN
			if (op)
				bd.children('.' + hc).mousedown();
			break;
		case 27:// ESC
			self.close();
			break;
		default:
			var v = tf.val(), lt = bd.children('.tr');
			if (!v || v.length < (acfg.minChars || 4)) {
				lt.show();
				break;
			}
			if (v != self.displayValue) {
				var dd = self.data;
				lt.each(function(n, c) {
					var ec = $(c), k = ec.attr('name')
						, b, x = dd[k][fb] + '';
					if (cfg.equals) {
						b = cfg.equals(x, k, v, dd);
					} else {
						b = x && x.indexOf(v) > -1;
					}
					ec.toggle(b);
				});
				if (!self.isOpen)
					self.open();
			}
		}
		return true;
	}).keydown(function(e) {
		switch (e.keyCode) {
		case 9: // TAB
			var v = tf.val();
			if (v && v != self.displayValue){
				var lt = bd.children('.tr:visible'), k;
				if (lt.length == 1) {
					k = lt.first().attr('name');
					v = self.data[k][fb];
				}
				
//				if (acfg.strict) {
					self.select(k);
//				} else {
//					if (k) {
//						self.select(k);
//					} else {
//						self.selected = [v];
//					}
//					self.close();
//				}
				return 1;
			}
			break;
		}
		return 1;
	});
	return self;
};
///<jscompress sourcefile="combo_grid.js" />
/**
 * Magic ComboBox Grid Plugin v1.0.1
 * author: Mac_J@boarsoft.com
 * need combo.js, core.js
 */
mac.combo_grid = function(self, cfg) {
	var hd = self.children('.head'), tf = hd.find('input')
		, btn = hd.children('.btn'), ico = btn.children('.icon')
		, bd = self.children('.body'), gd = self.config.boxEl;
	self.key = '';
	ico.removeClass('icon-triangle-1-s').addClass('icon-search');
	self.onOpen = cfg.onOpen;
	cfg = $.extend(self.config, cfg, {
		onOpen: function(me){
			gd.adjust(bd);
			gd.select(me.selected);
			if (self.onOpen) {
				self.onOpen();
			}
//			ico.removeClass('icon-triangle-1-s').addClass('icon-search');
		}
//		onClose: function(me){
//			ico.removeClass('icon-search').addClass('icon-triangle-1-s');
//		},
//		onSelect: function(){
//			self.key = tf.val();
//		}
	});
	$.extend(gd.config, {
		onRowClick: function(e){
			var ec = $(this), k = ec.attr('name');
			if (cfg.multiSelect) {
				if (gd.selected && ec.hasClass('selected')) {
					delete gd.selected[k];
					ec.removeClass('selected');
				} else {
					gd.selected = gd.selected || {};
					gd.selected[k] = gd.data[k];
					ec.addClass('selected');
				}
				var sl = [];
				$.each(gd.selected, function(n, c){
					sl.push(c);
				});
				self.select(sl);
			} else {
				gd.find('.tr').removeClass('selected');
				ec.addClass('selected');
				gd.selected = {};
				gd.selected[k] = gd.data[k];
				self.select(gd.data[k]);
				self.close();
			}
		},
		afterLoad: function(dd, po, me){
			self.data = dd;
			gd.select(self.selected);
			gd.find('.tr').first().addClass('hover');
		}
	});
	gd.select = function(sl){
		//sl = self.checkin(sl);
		var at = gd.find('.tr').removeClass('selected');
		$.each(sl, function(n, k){
			at.filter("[name='" + k + "']").addClass('selected');
		});
	}
	tf.unbind('keydown').keydown(function(e) {
		var k = e.keyCode, op = self.isOpen
			, rs = gd.find('.tr'), v = tf.val();
		switch (k) {
		case 9: //TAB
		case 27:// ESC
			self.close();
			tf.focus();
			break;
		case 38: // UP
		case 40: // DOWN
			var d = (k == 40), h = rs.filter('.hover')
				, s = rs.filter('.selected'), x, t, z;
			if (!op && d) {
				self.key = v;
				self.open();
			}
			if (h.length) {
				h = h.removeClass('hover').first();
				x = d? h.next().next():h.prev().prev();
			}
			if (!x || !x.length) {
				x = d? rs.first() : rs.last();
			}
			if (x.length) {
				x.addClass('hover');
				var yb = gd.find('.ybar');
				t = x.position().top;
				if (t < 28) {
					yb.scrollTop(yb.scrollTop() + t - 28);
				} else if (t > 253) {
					yb.scrollTop(yb.scrollTop() + t - 253);
				}
			}
			return false;
		case 13:// RETURN
			var h = rs.filter('.hover');
			if (op && h.length && v == self.key) {
				h.click();
				h.removeClass('hover');
				return false;
			}
			if (!op) {
				self.open();
			}
			if (v != self.key) {
				self.key = v;
				self.selected = [];
				gd.load({ key: v });
			}
			return false;
		}
		return true;
	});
	btn.unbind('click').click(function(){
		if (self.hasClass('disabled')) {
			return;
		}
		if (!self.isOpen) {
			self.open();
		}
		var v = tf.val();
		if (v == self.key) {
			return false;
		}
		self.key = v;
		delete self.selected;
		delete self.displayValue;
		gd.load({ key: v });
		return false;
	});
	return self;
};
///<jscompress sourcefile="contextMenu.js" />
// jQuery Context Menu Plugin
//
// Version 1.00
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
//
// Visit http://abeautifulsite.net/notebook/80 for usage and more information
//
// Terms of Use
//
// This software is licensed under a Creative Commons License and is copyrighted
// (C)2008 by Cory S.N. LaViska.
//
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//
/**
 * Version 2.4.1 Modified by Mac_J@boarsoft.com
 * 1. Added onPopup event.
 * 2. Fixed the position issue on popup.
 * 3. Button config for left click support.
 * 4. Auto position to fit screen.
 * 5. Multi-level submenu support.
 * 6. Reference menu object directly.
 * 7. upgrade for jquery2.x
 */
if(jQuery)( function($) {
$.extend($.fn, {
	contextMenu: function(o, callback) {
		var doc = document, de = doc.documentElement, db = doc.body, mu = o.menu;
		var popup = this.popup = function(el, e){
			//Mac_J
			if(o.onPopup!=null)
				o.onPopup(el);
			// Hide context menus that may be showing
			$(".contextMenu").hide().find('ul').hide();
			if( $(el).hasClass('disabled') ) return false;
			// Detect mouse position
			var d = {}, x, y;
			if( self.innerHeight ) {
				d.pageYOffset = self.pageYOffset;
				d.pageXOffset = self.pageXOffset;
				d.innerHeight = self.innerHeight;
				d.innerWidth = self.innerWidth;
			} else if( de && de.clientHeight ) {
				d.pageYOffset = de.scrollTop;
				d.pageXOffset = de.scrollLeft;
				d.innerHeight = de.clientHeight;
				d.innerWidth = de.clientWidth;
			} else if( db ) {
				d.pageYOffset = db.scrollTop;
				d.pageXOffset = db.scrollLeft;
				d.innerHeight = db.clientHeight;
				d.innerWidth = db.clientWidth;
			}
			if(!o.anchor || o.location=='mouse'){
				(e.pageX) ? x = e.pageX : x = e.clientX + d.scrollLeft;
				(e.pageY) ? y = e.pageY : x = e.clientY + d.scrollTop;
			}else if(o.anchor=='el'){
				x = el.position().left;
				y = el.position().top;
			}
			// Show the menu
			$(doc).unbind('click');
			//Mac_J
			function getAbsPoint(p) {
				var x = 0, y = 0;
				if($(p).css('position')=='absolute'){
					x = p.offsetLeft;
					y = p.offsetTop;
				}
				while (p = p.offsetParent) {
					if($(p).css('position')=='absolute'){
						x += p.offsetLeft;
						y += p.offsetTop;
					}
				}
				return { x: x, y: y };
			}
			//Mac_J：坐标修正
			var cw = de.clientWidth, ch = de.clientHeight;
			var ofs = o.offset || {};
			function adjust(x, y){
				var afs = getAbsPoint(mu.parent()[0]);
				x += -afs.x + ofs.x;
				y += -afs.y + ofs.y;
				//Mac_J：对超出屏幕的情况进行判断
				var w = mu.width(), h = mu.height();
				if(x + w > cw)
					x = cw - w;
				if(y + h > ch)
					y = ch - h;
				return { top: y, left: x - 4 };
			}
			var fx = adjust(x, y);
			mu.css(fx).fadeIn(o.inSpeed);
			// Hover events
			mu.find('li').mouseover(function() {
				var ec = $(this);
				var oc = ec.parent().find('li');
				oc.removeClass('hover').find('ul').hide();
				var sm = ec.addClass('hover').children('ul');
				if(sm.length > 0){
					if(!sm.attr('width')){
						var w = sm.width();
						sm.width(w);//important!
						sm.attr('width', w);
					}
					sm.show();
					var p = ec.position();
					var y = fx.top + p.top + sm.height() - ch;
					var x = fx.left + p.left + ec.width() + sm.width() - cw;
					//alert(fx.top + '/' + p.top + '/' + ec.height() + '/' + sm.height() + '/' + ch)
					sm.css({ 'top': y>0? -y-1:-1, 'left': x>0? -sm.width():ec.width() });
				}
				return false;
			}).mouseout( function() {
				mu.find('li').removeClass('hover');
				return false;
			});
			// Keyboard
			$(doc).keypress( function(e) {
				switch( e.keyCode ) {
					case 38: // up
						if( mu.find('LI.hover').size() == 0 ) {
							mu.find('LI:last').addClass('hover');
						} else {
							mu.find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
							if( mu.find('LI.hover').size() == 0 ) mu.find('LI:last').addClass('hover');
						}
					break;
					case 40: // down
						if( mu.find('LI.hover').size() == 0 ) {
							mu.find('LI:first').addClass('hover');
						} else {
							mu.find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
							if( mu.find('LI.hover').size() == 0 ) mu.find('LI:first').addClass('hover');
						}
					break;
					case 13: // enter
						mu.find('LI.hover span[action]').trigger('click');
					break;
					case 27: // esc
						$(doc).trigger('click');
					break
				}
			});
			// When items are selected
			mu.find('LI[action]').unbind('click');
			mu.find('LI:not(.disabled)[action]').click( function() {
				$(doc).unbind('click').unbind('keypress');
				$(".contextMenu").hide().find('ul').hide();
				// Callback
				if( callback ) callback( $(this).attr('action'), el, {x: x - ofs.left, y: y - ofs.top, docX: x, docY: y} );
				return false;
			});
			// Hide bindings
			setTimeout( function() { // Delay for Mozilla
				$(doc).click( function() {
					$(doc).unbind('click').unbind('keypress');
					mu.fadeOut(o.outSpeed, o.afterClose || $.noop);
					return false;
				});
			}, 0);
		};
		// Defaults
		if( o.button == undefined ) o.button = 3;//Mac_J, default = right click
		if( o.menu == undefined ) return false;
		if( o.inSpeed == undefined ) o.inSpeed = 150;
		if( o.outSpeed == undefined ) o.outSpeed = 75;
		// 0 needs to be -1 for expected results (no fade)
		if( o.inSpeed == 0 ) o.inSpeed = -1;
		if( o.outSpeed == 0 ) o.outSpeed = -1;
		// Loop each context menu
		$(this).each( function() {
			var el = $(this);
			var offset = $(el).offset();
			// Add contextMenu class
			mu.addClass('contextMenu');
			// Simulate a true right click
			$(this).mousedown( function(e) {
				var evt = e, el = $(this);
				el.mouseup( function(e) {
					$(this).unbind('mouseup');
					if( evt.which == o.button ) {
						popup(el, e);
					}
					return false;
				});
				return false;
			});
			// Disable text selection
			//if( $.browser.mozilla ) {
				mu.each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
			//} else if( $.browser.msie ) {
				mu.each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
			//} else {
				mu.each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
			//}
			// Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
			$(el).add('UL.contextMenu').bind('contextmenu', function() { return false; });
		});
		return $(this);
	},
	// Disable context menu items on the fly
	disableItems: function(o, c) {
		var me = $(this);
		c = c ? c:'disabled';
		if(!o || o == '*') {
			me.find('LI').addClass(c);
			return me; // Disable all
		}
		$(this).each( function() {
			var d = o.split(',');
			for (var i = 0; i < d.length; i++) {
				me.find('LI[action="' + d[i] + '"]').addClass(c);
			}
		});
		return me;
	},
	// Enable context menu items on the fly
	enableItems: function(o, c) {
		var me = $(this);
		c = c ? c:'disabled';
		if(!o || o == '*') {
			me.find('LI.' + c).removeClass(c);
			return me; // Enable all
		}
		$(this).each( function() {
			var d = o.split(',');
			for (var i = 0; i < d.length; i++) {
				me.find('LI[action="' + d[i] + '"]').removeClass(c);
			}
		});
		return me;
	},
	// Disable context menu(s)
	disable: function() {
		$(this).each( function() {
			$(this).addClass('disabled');
		});
		return( $(this) );
	},
	// Enable context menu(s)
	enable: function() {
		$(this).each( function() {
			$(this).removeClass('disabled');
		});
		return( $(this) );
	},
	// Destroy context menu(s)
	destroyContextMenu: function() {
		// Destroy specified context menus
		$(this).each( function() {
			// Disable action
			$(this).unbind('mousedown').unbind('mouseup');
		});
		return( $(this) );
	}
});
})(jQuery);;
///<jscompress sourcefile="dialog.js" />
/**
 * Magic dialog 2.5.2
 * Author: Mac_J@boarsoft.com
 * need: core.js
 */
mac.dialog = function(self, cfg) {
	if (cfg.width)
		self.width(cfg.width);
	if (cfg.height)
		self.height(cfg.height);
	self.hide();
	var mk = $('<div class="mask"></div>');
	var hd = $('<div class="head"></div>').appendTo(self);
	var bd = self.body = $('<div class="body"></div>');
	self.adjust = function() {
		var doc = $(document);
		var docEl = document.documentElement;
		var cw = docEl.clientWidth, ch = docEl.clientHeight;
		var dh = doc.height(), dw = doc.width();
		mk.height(dh > ch ? dh : ch).width(dw);
		if ((!cfg.pos || cfg.pos == 'center') && !self.moved) {
			var w = self.width(), h = self.height();
			cw = docEl.clientWidth;
			ch = docEl.clientHeight;
			l = cw / 2 - w / 2;
			t = ch / 2 - h / 2;
			l = l < 0 ? 0 : l;
			t = t < 0 ? 0 : t;
			self.css({
				left : cfg.left || l,
				top : cfg.top || t
			});
			if(cfg.resizable && h>ch) {
				self.height(ch);
				bd.height(ch - hd.height() - 14);
				if(!cfg.resizable && self.width() == w)
					self.width(w + 24);
			}
		}
		return self;
	}
	self.close = function() {
		if (cfg.closeAction == 'remove') {
			self.remove();
			mk.remove();
		} else {
			self.hide();
			mk.hide();
		}
		$(window).unbind('keydown', self.close);
		if (cfg.onClose)
			cfg.onClose();
	}
	$(window).resize(self.adjust);
	if (cfg.blank)
		return self;
	// setting tabIndex makes the div focusable
	// setting outline to 0 prevents a border on focus in Mozilla
	self.attr('tabIndex', 0).css('outline', 0);
	self.click(function(e) {
		self.move2top();
	});
	hd.append('<span class="fl">' + cfg.title + '</span>');
	var hdr = $('<span class="fr"></span>').appendTo(hd);
	if (!cfg.noCloser) {
		var c = $('<span class="icon icon-close closer"></span>');
		c.click(self.close);
		hdr.append(c);
	}
	
	// alert(hd.height());
	// bd.height(self.height() - hd.height());
	if (cfg.el)
		bd.append(cfg.el);
	self.append(bd);
	self.move2top = function() {
		var ec = $(this), z = ec.css('z-index');
		var d = mac.dialog;
		if (z && !isNaN(z) && z >= d.maxZ) {
			d.maxZ = z;
		} else {
			if (cfg.model) {
				mk.css('z-index', ++d.maxZ);
			}
			ec.css('z-index', ++d.maxZ);
		}
		mk.show();
		return self.show();
	}
	if (cfg.draggable) {
		self.draggable({
			cancel : '.body, .closer',
			handle : '.head',
			containment : 'window',
			stop : function() {
				self.moved = true;
			}
		});
		self.css('position', 'absolute');
	}
	if (cfg.resizable) {
		// .ui-resizable has position: relative defined in the stylesheet
		// but dialogs have to use absolute or fixed positioning
		// var p = self.css('position');
		self.resizable({
			cancel : '.body',
			// containment: self.parent(),
			// alsoResize: self.find('.body'),
			maxWidth : cfg.maxWidth,
			maxHeight : cfg.maxHeight,
			minWidth : cfg.minWidth,
			minHeight : cfg.minHeight,
			handles : cfg.handles,
			resize : function() {
				var h = self.height() - hd.height();
				bd.height(h);
				if (cfg.onResize)
					cfg.onResize(bd.width(), h);
			}
		});
		self.css('position', 'absolute');
	}
	self.open = function() {
		if (self.parent().length == 0)
			$('body').prepend(self);
		if (cfg.model)
			$('body').prepend(mk.show());
		self.addClass('dialog');
		self.move2top();// 移动到最前
		self.adjust().show();
		self.focus();
		if (cfg.closeOnEsc) {
			$(window).keydown(function(e) {
				var z = self.css('z-index');
				if (e.keyCode == 27 && z == mac.dialog.maxZ)
					self.close();
			});
		}
	}
	if (cfg.autoOpen)
		self.open();
	return self;
}
//set default z-index
mac.dialog.maxZ = 1000;
mac.loader = function(pg) {
	var dg = $('<div><div>').mac('dialog', {
		title : 'loading',
		width : 240,
		height : 120,
		autoOpen : true,
		draggable : true,
		noCloser : true,
		model : true,
		closeAction : 'remove'
	});
	dg.body.append('<div class="wait"></div>');
	var chkFunc = function(){
		if (pg.loading > 0) {
			window.setTimeout(chkFunc, 500);
		}
		dg.close();
	}
	window.setTimeout(chkFunc, 500)
	return dg;
}
mac.wait = function(msg, params, w, h) {
	var dg = $('<div><div>').mac('dialog', {
		title : mac.msg(msg, params),
		width : w || 240,
		height : h || 120,
		autoOpen : true,
		draggable : true,
		noCloser : true,
		model : true,
		closeAction : 'remove'
	});
	dg.body.append('<div class="wait"></div>');
	return dg;
}
mac.opendlg = function(c) {
	var dg = $('<div></div>').mac('dialog', {
		title : c.title,
		width : c.width || 400,
		height : c.height || 'auto',
		autoOpen : true,
		draggable : true,
		noCloser : true,
		model : true,
		closeAction : 'remove'
	});
	var ct = $('<div class="main"></div>');
	ct.append(c.el || mac.msg(c.message, c.params));
	dg.body.append(ct);
	if (c.buttons && c.buttons.length > 0) {
		var bb = $('<div class="bottom"></div>');
		$.each(c.buttons, function(n, c) {
			var b = $('<div class="button"></div>');
			b.append(c.text).click(function() {
				c.click(dg);
				return false;
			});
			if (c.width)
				b.width(c.width);
			bb.append(b);
		});
		bb.append('<div class="clear"></div>');
		dg.body.append(bb)
	}
	// var mh = dg.height() - bb.height() - dg.children('.head').height();
	// ct.height(mh);
	dg.keydown(function(e) {
		switch (e.keyCode) {
		case 13:
			dg.close();
			return false;
		case 27:
			dg.close();
			return false;
		}
	});
	return dg;
}
mac.confirm = function(msg, callback, params, btns) {
	return mac.opendlg({
		title : '',
		message : msg,
		callback : callback,
		params : params,
		buttons : btns || [ {
			text : mac.Msg.ok || 'OK',
			click : function(d) {
				d.close();
				callback();
			}
		}, {
			text : mac.Msg.cancel || 'Cancel',
			click : function(d) {
				d.close();
			}
		} ]
	});
};
mac.alert = function(msg, params, callback) {
	return mac.opendlg({
		title : '',
		message : msg,
		callback : callback,
		params : params,
		buttons : [ {
			text : mac.Msg.ok || 'OK',
			click : function(d) {
				d.close();
				if (callback)
					callback();
			}
		} ]
	});
};;
///<jscompress sourcefile="dtree.js" />
/**
 * Magic dtree 2.5.1
 * Author: Mac_J@boarsoft.com
 * need: dynatree.js
 */
mac.dtree = function(self, cfg) {
	self = $.extend(self, cfg);
	self.rootNode = null;
	self.setContextMenu = function(span) {
		if(!self.ctxMenu || self.ctxMenu.length!=1)
			return;
//		var df = self.find('.ui-dynatree-document, .ui-dynatree-folder');
		var df = self.find('.dynatree-node, .dynatree-folder');
		//var df = $(span);
		df.contextMenu({
			menu : self.ctxMenu,
			onPopup: self.onCtxMenu,
			offset: self.menuOffset
		}, function(act, el, pos) {
			var dt = $.ui.dynatree.getNode(el);
			//var dt = el.context.dtnode;
			//var dt = el.prop('dtnode');
			dt.activate(true);
			self.onCtxMIClick[act](self, dt, el, pos);
		});
	};
	self.addNode = function(dd){
		var nn, pn = self.getNode(dd.parent);
		if (!pn)
			return;
		pn.data.isFolder = true;
		pn.data.childCount++;
		nn = pn.addChild(dd);
		pn.expand(true);
		self.setContextMenu();
		return nn;
	}
	self.updateNode = function(dd){
		var dt = self.getNode(dd.id || dd.key);
		dt.data = dd;
		dt.render();
		self.setContextMenu();
		return dt;
	}
	self.deleteNode = function(key){
		var pn = self.getNode(key);
		if(!pn)
			return;
		var pt = self.getNode(pn.data.parent);
		pt.data.childCount--;
		if (pt.data.childCount == 0) {
			pt.data.isFolder = false;
			pt.data.isLazy = false;
		}
		pn.remove();
	}
	self.deleteChildren = function(dt) {
		dt.visit(function(n){
			n.remove();
		});
		dt.data.isFolder = false;
		dt.data.isLazy = false;
		dt.data.childCount = 0;
	}
	self.expandNode = function(dt, dd) {
		if(self.beforeLoad) {
			self.beforeLoad(dt, dd);
		}
		dt.removeChildren();
		dt.addChild(dd);
		dt.expand();
		self.setContextMenu();
		if(self.afterLoad) {
			self.afterLoad(dt, dd);
		}
	}
	self.loadSubTree = function(dt) {
		var p = $.extend(self.loader.params || {}, {
			id: dt.data.key
		});
		$.post(self.loader.url, p, function(rs) {
			dt.setLazyNodeStatus(DTNodeStatus_Ok);
			var ro = mac.eval(rs);
			if (!ro.success) {
				dt.setLazyNodeStatus(DTNodeStatus_Error);
				return;
			}
			self.expandNode(dt, ro.data);
		});
	};
	self.treeConfig = $.extend(true, {
		fx : {
			height : 'toggle',
			duration : 200
		},
		onLazyRead : function(dt) {
			self.loadSubTree(dt);
		},
		onClick : function(dt, event) {
			var a = self.ctxMenu;
			if (a && a.is(':visible') && a.css('display')!='none')
				return false;
			return true;
		},
		onActivate : function(dt) {
			if(self.onActivate)
				self.onActivate(dt);
		}
	}, self.treeConfig);
	self.dynatree(self.treeConfig);
	//
	self.getTree = function(){
		return self.dynatree('getTree');
	}
	self.getNode = function(k){
		return self.getTree().getNodeByKey(k);
	}
	self.rootNode = self.dynatree('getRoot');
	//
	self.expand2 = function(p, nf){
		p = '/_1' + p;
		self.getTree().loadKeyPath(p, function(dt, st){
			if (st == "loaded") {
				dt.expand();
			} else if (st == "ok") {
				dt.activate();
				dt.expand();
			} else if (st == "notfound") {
				//self.rootNode.expand();
				nf && nf(p);
			}
		});
	}
	return self;
};;
///<jscompress sourcefile="grid.js" />
/**
 * Magic grid 3.1
 * author: Mac_J@boarsoft.com
 * need: core.js, mousewheel.js
 */
(function($){
mac.grid = function(self, cfg) {
	cfg.rowHeight = cfg.rowHeight || 24;
	cfg.rowLine = cfg.rowLine>=0 ? cfg.rowLine : 1;
	self.config = cfg;
	self.loader = cfg.loader || {};
	self.data = {};
	var yb = $('<div class="ybar"></div>').appendTo(self);
	var yy = $('<div></div>').appendTo(yb);
	var mm = $('<div class="main"></div>').appendTo(self);
	var hd = $('<div class="head"></div>').appendTo(mm);
	mm.append('<div class="clear"></div>');
	var bd = $('<div class="body"></div>').appendTo(mm);
	var tt = $('<div class="tt"></div>').appendTo(bd);
	var pb = self.loader.params || {};
	pb = pb.pageNo && pb.pageSize && cfg.pagerLength;
	if (pb)
		pb = $('<div class="pager"></div>').appendTo(self);
	var _h, _s;
	$('body').mousemove(function(e) {
		if (_s) {
			var dx = e.clientX - _s.mouseX;
			var cd = _s.th;
			var nw = cd.width() + dx;
			if (nw > 0) {
				bd.width(bd.width() + dx);
				tt.width(bd.width());
				hd.width(hd.width() + dx);
				_s.mouseX = e.clientX;
				mm.find('[name="' + _s.name + '"]').width(nw);
			}
		}
		if (_h) {
			var p = mac.getMousePos(e);
			_h.css({
				left : p.x + 5,
				top : p.y + 12
			});
		}
	}).mouseup(function(e) {
		if (_s){
			$.each(cfg.cols, function(n, c){
				if(c.name == _s.name)
					c.width = hd.seek(c.name).width();
				if(_s.group && c.name == _s.group)
					c.width = hd.seek(_s.group).width();
			});
			_s = 0;
		}
		if (_h) {
			_h.remove();
			_h = 0;
		}
	});
	self.sortBy = [];
	var tw = 0;
	$.each(cfg.cols, function(n, c) {
		var f = c.name = c.name || c.field;
		var o = $('<div></div>').attr('name', f);
		var p = c.parent || hd;
		o.appendTo(p);
		if(c.parent){
			o.attr('group', c.group);
			o.parents('.th').css('border-right', 'none');
		}
		if (c.hidden) {
			o.hide();
			return;
		}
		if(!c.isGroup)
			tw += c.width + (c.parent?1:2);
		o.attr('unselectable', 'on');
		if (c.sizeable != false && !c.isGroup) {
			var rh = $('<div class="sizer" unselectable="on"></div>');
			rh.mousedown(function(e) {
				var fcs = cfg.fixedCols || [];
				if ($.inArray(c.field, fcs) < 0
					&& $.inArray(c.group, fcs) < 0) {
					var th = hd.find('[group=' + f + ']').last();
					if(th.length==0)
						th = hd.seek(f);
					_s = {
						th: th,
						name: th.attr('name'),
						group: th.attr('group'),
						mouseX : e.clientX
					}
				}
				return false;
			}).appendTo(o);
		}
		o.width(c.width);
		if (c.sort) {
			var u = $('<span class="icon icon-triangle-1-s"></span>');
			$('<div class="sort"></div>').append(u).appendTo(o);
			self.sortBy.push({
				field : f,
				sort : "asc"
			});
		}
		var a = c.header;
		if (!a) {
			a = $('<div class="title" unselectable="on"></div>').append(c.title);
			var x = (c.parent) ? (c.height || cfg.rowHeight) : (cfg.headerHeight || cfg.rowHeight);
			a.height(x).css('line-height', x + 'px');
		}
		if (c.moveable != false) {
			a.mousedown(function(e) {
				if (_s)
					return;
				var fcs = cfg.fixedCols || [];
				if ($.inArray(c.field, fcs) < 0
						&& $.inArray(c.group, fcs) < 0) {
					_h = $('<div class="dragHelper"><div>');
					_h.html('&nbsp;' + c.title + '&nbsp;');
					_h.col = $.inArray(f, $.map(cfg.cols, function(c) {
						return c.name || c.field
					}));
					_h.name = f;
					$('body').append(_h);
					var p = mac.getMousePos(e);
					_h.css({
						left : p.x + 5,
						top : p.y + 12
					});
				}
				return false;
			}).mousemove(function(e) {
				if (_h && f != _h.name) {
					var td = bd.find('.td[name=' + f + ']');
					td.addClass("dropable")
				}
			}).mouseout(function(e) {
				var td = bd.find('.td[name=' + f + ']');
				td.removeClass("dropable");
			}).mouseup(function() {
				var fcs = cfg.fixedCols || [];
				if ($.inArray(f, fcs) >= 0)
					return true;
				if (_h && f != _h.name) {
					if (hd.seek(_h.name).attr('group') != o
							.attr('group')) {
						_h.remove();
						return;
					}
					var td = mm.find('.td[name=' + f + '],.th[name=' + f + ']');
					td.removeClass("dropable");
					$.each(td, function(n, c) {
						var s = $(c);
						var t = s.parent().find('.td[name=' + _h.name
							+ '],.th[name=' + _h.name + ']');
						s.before(t);
					});
					var k = $.inArray(f, $.map(cfg.cols, function(c) {
						return c.name || c.field;
					}));
					cfg.cols.splice(k, 0, cfg.cols[_h.col]);
					cfg.cols.splice(_h.col + 1, 1);
					_h.remove();
				}
			});
		}
		o.addClass('th').append(a);
	});
	hd.width(tw-2);
	bd.width(tw-1);
	tt.width(tw-2);
	self.sort = function() {
		var sb = self.sortBy;
		if (cfg.sortLocally) {
			var dd = [];
			$.each(self.data, function(n, c) {
				dd.push(c);
			});
			dd.sort(function(a, b) {
				for ( var i = 0; i < sb.length; i++) {
					var k = sb[i].field;
					var x = a[k];
					var y = b[k];
					var cp = null;
					for ( var j = 0; j < cfg.cols.length; j++) {
						var cc = cfg.cols[j];
						if (cc.field == k) {
							cp = cc.comparator;
							break;
						}
					}
					if (cp)
						return cp(x, y, sb[i].sort, a, b);
					if (x == y) {
						continue
					} else {
						if (sb[i].sort == 'desc') {
							return x > y ? 1 : -1
						} else {
							return x < y ? 1 : -1
						}
					}
				}
				return 0
			});
			self.loadData(dd);
		} else {
			var ob = $.map(sb, function(c) {
				return c.field + ' ' + c.sort
			}).join(',');
			self.load({
				orderBy : ob
			});
		}
	}
	hd.find('.sort').click(function() {
		var el = $(this).hide();
		var tu = 'icon-triangle-1-n';
		var td = 'icon-triangle-1-s';
		var fd = el.parent().attr('name');
		var sp = el.children();
		var sb = self.sortBy;
		for ( var i = 0; i < sb.length; i++) {
			if (sb[i].field == fd)
				break;
		}
		sb.splice(i, 1);
		if (sp.hasClass(tu)) {
			sb = sb.unshift({
				field : fd,
				sort : 'asc'
			});
			sp.removeClass(tu).addClass(td)
		} else {
			sb = sb.unshift({
				field : fd,
				sort : 'desc'
			});
			sp.removeClass(td).addClass(tu)
		}
		self.sort();
		return false;
	});
	self.adjustYBar = function() {
		yb.height(bd.height());
		yy.height(tt.height() + cfg.rowHeight);
		yb.css('overflow-y', (yy.height() > yb.height()) ? 'scroll' : 'auto');
	}
	self.adjust = function(vp) {
		vp = vp || self.parent();
		var h = vp.height();
		self.height(h);
		var g = pb ? 32 : 0;
		h = h - hd.height();
		bd.height(h - g - cfg.rowHeight - (cfg.footerHeight || 0));
		self.adjustYBar();
		if(self.afterAdjust)
			self.afterAdjust();
	};
	self.newRow = function(r, k) {
		var k = cfg.key ? r[cfg.key] : 'r' + k;
		self.data[k] = r;
		var a = $('<div name="' + k + '" class="tr"></div>');
		a.height(cfg.rowHeight);
		$.each(cfg.cols, function(n, c) {
			var b = $('<div class="td"></div>');
			b.height(cfg.rowHeight);
			b.attr('name', c.name || c.field);
			if (c.group) {
				var p = a.find('.td[name=' + c.group + ']');
				p.css('border-right', 'none');
				p.append(b);
				b.attr('group', c.group);
			} else {
				b.appendTo(a);
			}
			if (!c.isGroup) {
				b.append((c.render) ? c.render(r, a, self, b) : r[c.field]);
				b.append('&nbsp;').css('text-align', c.align || 'left');
				b.width(c.width);
			}
			if (c.hidden)
				b.hide();
		});
		a.click(cfg.onRowClick || function() {
			self.find('.tr').removeClass('selected');
			self.selected = $(this).addClass('selected');
		}).hover(function() {
			tt.children('.tr').removeClass('hover');
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		}).append('<div class="clear"></div>');
		return a;
	}
	self.addRow = function(r, k) {
		var a = self.newRow(r, k), h = tt.height();
		tt.append(a).append('<div class="clear"></div>');
		// tt auto height?
		// tt.height(h + a.height() + (cfg.rowLine * 1));
		self.adjustYBar();
	}
	self.delRow = function(keys) {
		keys = keys || [];
		if (keys.length == 0) {
			var s = self.selected;
			if (s)
				keys.push(s.attr('name'));
			self.selected = null;
		}
		$.each(keys, function(n, k) {
			if (self.data[k])
				delete self.data[k];
			var tr = tt.children('.tr[name=' + k + ']');
			tr.next().remove();
			tr.remove();
			// tt.height(tt.height() - tr.height() - (cfg.rowLine * 1));
		});
		self.adjustYBar();
		return keys;
	}
	self.loadData = function(dd, po) {
		self.data = {};
		tt.empty();
		yy.height(2);
		$.each(dd, function(n, r) {
			self.addRow(r, n);
		});
		tt.find('.tr:odd').addClass('h2o');
		tt.find('.tr:even').addClass('h2e');
		hd.find('.sort').show();
		if (!cfg.autoHeight)
			self.adjust();
		if (cfg.afterLoad)
			cfg.afterLoad(dd, po, self);
		return self;
	}
	self.load = function(pms, url) {
		pms = pms || {};
		var ldr = self.loader;
		$.extend(ldr.params, pms);
		$.post(url || ldr.url, ldr.params, function(data) {
			var ro = mac.eval(data);
			if (ro.success) {
				ro = ro.data;
				self.loadData(ro.list, ro);
				if (pb) {
					pb.update(ro.total, ro.pageSize, ro.pageCount, ro.pageNo);
				}
			}
			hd.find('.sort').show();
		})
	};
//	// 扩展获取选中行的主键集合，必须制定key选项
//	self.selectedKeys = function() {
//		var keys = [];
//		bd.find('.selected').each(function() {
//			var key = $(this).attr("name");
//			keys.push(key);
//		});
//		return keys;
//	};
	yb.scroll(function() {
		bd.scrollTop(this.scrollTop)
	});
	bd.mousewheel(function(e, delta, deltaX, deltaY) {
		yb.scrollTop(yb.scrollTop() - deltaY * cfg.rowHeight);
		return false;
	});
	if (pb) {
		self.pager = pb.mac('pager', {
			pagerLength : cfg.pagerLength,
			pageSize : self.loader.pageSize,
			loadPage : function(pn) {
				self.load({
					pageNo : pn
				});
			}
		});
	}
	if (self.loader.autoLoad) {
		self.load();
	} else if (!cfg.autoHeight) {
		self.adjust();
	}
	return self;
}
})(jQuery);;
///<jscompress sourcefile="grid_fixCol.js" />
/**
 * MagicGrid FixCol Plugin v2.0.1
 * author: Mac_J@boarsoft.com
 * need: grid.js
 */
(function($){
mac.grid_fixCol = function(self) {
	var cfg = self.config;
	// left panel for fixed columns
	var fm = $('<div class="fixed"></div>');
	var fh = $('<div class="head"></div>');
	var fb = $('<div class="body"></div>');
	fm.append(fh);
	fm.append('<div class="clear"></div>');
	fm.append(fb)
	// get elements from grid
	var yb = self.children('.ybar');
	yb.after(fm);
	var mm = self.children('.main');
	var hd = mm.children('.head');
	var bd = mm.children('.body');
	var tt = bd.children('.tt');
	// set col.hidden for reload data
	function toggleColCfg(fns, b) {
		if (!fns || fns.length < 1)
			return;
		$.each(cfg.cols, function(n, c) {
			if ($.inArray((c.name || c.field), fns) > -1) {
				c.hidden = b;
				c.fixed = b;
				return false;// end each
			}
		});
	}
	// clone fixed columns to left panel
	// ift: is first time
	function cloneCols(fns, ift) {
		fh.empty();
		fb.empty();
		var rs = tt.children(), fc = rs.clone(true);
		fc.children('.td').hide();
		$.each(fns, function(n, c) {
			var q = '[name=' + c + ']';
			var h = hd.children('.th' + q);
			var hc = h.clone(true).css('display', 'inline-block');
			hc.find('.sizer').hide();
			h.hide();
			fh.append(hc);
			var x = rs.find('.td' + q).hide();
			fc.children('.td' + q).css('display', 'inline-block');
			var w = ift ? x.width() : 0;
			hd.width(hd.width() - w);
			bd.width(bd.width() - w);
			tt.width(tt.width() - w);
		});
		fb.append(fc);
		fc.children('.td:hidden').remove();
		self.find('.tr').click(function(){
			if(cfg.onRowClick){
				cfg.onRowClick();
				return;
			}
			self.find('.tr').removeClass('selected');
			var tr = self.find('.tr[name=' + $(this).attr('name') + ']');
			self.selected = tr.addClass('selected');
		}).hover(function(){
			self.find('.tr[name=' + $(this).attr('name') + ']').addClass('hover');
		}, function(){
			self.find('.tr[name=' + $(this).attr('name') + ']').removeClass('hover');
		});
	}
	// fns: field names
	self.lockCols = function(fns) {
		fm.show();
		self.fixedCols = fns;
		toggleColCfg(fns, true);
		cloneCols(fns, true);
		// 代理afterLoad事件
		var ual = cfg.afterLoad;
		cfg.afterLoad = function(dd, po, self) {
			cloneCols(fns, false);
			if (ual) {
				ual(dd, po, self);
			}
		}
	}
	self.unlockCols = function() {
		var w = fm.width();
		fh.children().each(function(n, c) {
			hd.seek($(c).remove().attr('name')).show();
		});
		fb.children('.tr').each(function(n, c) {
			var ec = $(c), a = ec.attr('name');
			var x = tt.children('.tr[name=' + a + ']');
			ec.children('.td').each(function(m, t) {
				x.children('.td[name=' + $(t).attr('name') + ']').
					css('display', 'inline-block');
			});
			ec.children('.td:last').removeClass('brx');
		});
		fb.empty();
		hd.width(hd.width() + w);
		bd.width(bd.width() + w);
		tt.width(tt.width() + w);
		toggleColCfg(self.fixedCols, false);
		self.fixedCols = [];
		fm.hide();
	}
	// event
	yb.scroll(function() {
		fb.scrollTop(this.scrollTop)
	});
	self.afterAdjust = function(){
		fb.height(bd.height());
	}
}
})(jQuery);;
///<jscompress sourcefile="grid_keyEvt.js" />
/**
 * MagicGrid KeyEvent Plugin v1.0
 * author: Mac_J@boarsoft.com
 * need: grid.js, core.js
 */
mac.grid_keyEvt = function(self, cfg) {
	cfg = $.extend(self.config, {
		keyEvt : $.extend({
			tabIndex : 1
		}, cfg)
	});
	var mm = self.children('.main');
	var hd = mm.children('.head');
	var bd = mm.children('.body');
	var tt = bd.children('.tt');
	var acfg = cfg.keyEvt;
	tt.keyup(function(e) {
		var tg = $(e.target), td = tg.closest('.td');
		var tr = td.parent(), v = ':visible';
		switch (e.keyCode) {
		case 13: // RETURN
		case 9: // TAB
			var sti = acfg.tabIndex, d = e.shiftKey? -1:1;
			var ti = tg.attr('tabIndex');
			ti = ti ? ti + d : sti;
			var a = tr.find('[tabIndex=' + ti + ']');
			while (a.length && !a.is(':visible')){
				ti += d;
				a = tr.find('[tabIndex=' + ti + ']');
			}
			if (a.length && a.is(':visible')) {
				a.focus();
			} else {
				var s = '.tr' + v;
				var t = (d<0? tr.prevAll(s):tr.nextAll(s)).first();
				if (t.length) {
					t.click();
					t.find(v + '[tabIndex=' + sti + ']').focus();
				}
			}
			return false;
		}
		return true;
	}).keydown(function(e) {
		if (e.keyCode == 9)
			return false;
	});
};
///<jscompress sourcefile="input_autoCp.js" />
/**
 * Magic Input Auto Complete Plugin 1.0
 * Author: Mac_J@boarsoft.com
 * Need: core.js
 */
mac.input_autoCp = function(self, cfg) {
	var sc = 'selected', hc = 'hover', bd = self.next();
	if (cfg.boxHeight) {
		bd.height(cfg.boxHeight);
	}
	bd.mousedown(function(e) {
		e.stopPropagation();
		return false;
	});
	self.close = function() {
		self.isOpen = false;
		bd.hide();
	}
	self.open = function() {
		self.isOpen = true;
		var w = cfg.boxWidth || self.width()
			, p = self.position(), bt = cfg.boxTop, bl = cfg.boxLeft;
		bt = (bt || bt == 0) ? bt : (cfg.height + 2);
		bl = (bl || bl == 0) ? bl : 1;
		bd.css({
			top : p.top + bt,
			left : p.left + bl
		});
		bd.width(Math.max(w, cfg.boxMinWidth || w)).show();
		if (cfg.onOpen)
			cfg.onOpen();
		$(document).one('mousedown', self.close);
	}
	self.loadData = function(dd) {
		bd.empty();
		if (!dd || !dd.length)
			return;
		$.each(dd, function(i, r) {
			var a = $('<div class="tr"></div>').append(r);
			a.mousedown(function() {
				self.val(r);
				self.close();
			}).hover(function() {
				bd.children().removeClass(hc);
				$(this).addClass(hc);
			}, function() {
				$(this).removeClass(hc);
			});
			bd.append(a);
		});
		if (cfg.onLoad)
			cfg.onLoad(self, dd);
	}
	var ldr = cfg.loader;
	if (cfg.data) {
		self.loadData(cfg.data);
	} else if (ldr && ldr.url) {
		$.post(ldr.url, ldr.params, function(data) {
			var ro = mac.eval(data);
			if (ro.success) {
				ro = ro.data;
				self.loadData(ro.list);
			}
		});
	}
	self.keydown(function(e) {
		var k = e.keyCode, op = self.isOpen;
		var rs = bd.children('.tr'), v = ':visible';
		switch (k) {
		case 9: // TAB
			self.close();
			break;
		case 38: // UP
		case 40: // DOWN
			var x, d = (k == 40);
			if (!op && d)
				self.open();
			rs = rs.filter(v);
			var h = rs.filter('.' + hc), s = rs.filter('.' + sc);
			rs.removeClass(hc);
			var hx = (d ? h.nextAll(v) : h.prevAll(v)).first();
			var sx = (d ? s.nextAll(v) : s.prevAll(v)).first();
			if (h.length + s.length == 0 || hx.length + sx.length == 0) {
				x = (d ? rs.first() : rs.last());
			} else if (hx.length) {
				x = hx;
			} else if (sx.length && op && !h.length) {
				x = sx;
			} else if (s.length && !op) {
				x = s;
			} else {
				x = (d ? rs.first() : rs.last());
			}
			x.addClass(hc);
			return false;
		case 13:// RETURN
			if (op) {
				bd.children('.' + hc).mousedown();
			}
			break;
		case 27:// ESC
			self.close();
			break;
		default:
			var v = self.val(), lt = bd.children();
			if (!v || v.length < (cfg.minChars || 4)) {
				lt.show();
				break;
			}
			lt.each(function(n, c) {
				var ec = $(c), x = ec.text();
				ec.toggle(x && x.indexOf(v) > -1);
			});
			if (!self.isOpen)
				self.open();
		}
		return true;
	});
	return self;
};
///<jscompress sourcefile="menu.js" />
/**
 * Magic Menu 1.2
 * Author: Mac_J@boarsoft.com
 * Need: core.js, contextMenu.js
 */
(function($){
mac.menu = function(self, cfg) {
	cfg = $.extend({
		itemHeight : 24,
		marginLeft : 24
	}, cfg);
	self.addItems = function(p, items, l) {
		$.each(items, function(n, c) {
			var a = $('<li></li>'), x = c.icon || 'icon-none';
			if (c.style)
				a.addClass(c.style);
			a.attr('level', l);
			var lc = $('<span class="icon"></span>').addClass(x);
			if (c.children) {
				var rc = $('<span class="icon icon-triangle-1-e"></span>');
				rc.css('float', 'right').appendTo(a);
				var m = $('<ul></ul>');
				if(c.submenuWidth){
					//m.attr('width', c.submenuWidth);
					m.width(c.submenuWidth);
				}
				self.addItems(m, c.children, l + 1);
				a.append(m);
			}
			var tt = $('<span class="title"></span>').append(c.title);
			a.append(lc).append(tt).attr('action', c.action);
			a.height(cfg.itemHeight);
			p.append(a);
		});
	}
	self.addItems(self, cfg.children, 1);
	return self.hide();
}
})(jQuery);;
///<jscompress sourcefile="pager.js" />
/**
 * Magic grid pager plugin 1.1
 * Author: Mac_J@boarsoft.com
 * Need: core.js
 */
mac.pager = function(self, config) {
	self.pagerLength = config.pagerLength;
	self.loadPage = config.loadPage;
	self.lg = '<span class="ui-state-default">&gt;&gt;|</span>';
	self.ng = '<span class="ui-state-default">&gt;&gt;</span>';
	self.pg = '<span class="ui-state-default">&lt;&lt;</span>';
	self.fg = '<span class="ui-state-default">|&lt;&lt;</span>';
	// self.lp = "";
	self.np = '<span class="ui-state-default">&gt;</span>';
	self.pp = '<span class="ui-state-default">&lt;</span>';
	// self.fp = "";
	//
	self.update = function(total, pageSize, pageCount, pageNo) {
		self.empty();
		//
		self.total = total;
		self.pageCount = pageCount;
		self.pageNo = pageNo;
		self.pageSize = pageSize;
		//
		self.append(mac.msg(Msg.pager.page, [ self.total, self.pageNo,
				self.pageCount ]));
		self.append('&nbsp;');
		//
		var hf = Math.floor(self.pagerLength / 2);
		var hm = self.pagerLength % 2;
		var start = self.pageNo - hf;
		if (start > self.pageCount - self.pagerLength + 1) {
			start = self.pageCount - self.pagerLength + 1;
		}
		if (start < 1) {
			start = 1;
		}
		var end = start + self.pagerLength;
		if (end > self.pageCount + 1) {
			end = self.pageCount + 1;
		}
		if (start > 1) {
			var a = $('<span action="1" class="pageNo"></span>');
			a.append(self.fg).appendTo(self);
		}
		if (start > self.pagerLength) {
			var a = $('<span class="pageNo"></span>');
			var h = start - hf + (hm > 0) ? 0 : 1;
			a.attr('action', h).append(self.pg);
		}
		for ( var i = start; i < end; i++) {
			var a = $('<span class="pageNo"></span>');
			if (i != self.pageNo)
				a.attr('action', i);
			a.append(i).appendTo(self);
		}
		if (end <= self.pageCount - self.pagerLength) {
			var a = $('<span class="pageNo"></span>');
			a.attr('action', (end + hf)).append(self.ng).appendTo(self);
		}
		if (end < self.pageCount) {
			var a = $('<span class="pageNo"></span>');
			a.attr('action', self.pageCount).append(self.lg).appendTo(self);
		}
		self.children('span[action]').click(function() {
			self.pageNo = $(this).attr('action');
			self.loadPage.call(self, self.pageNo, self.pageSize);
		});
		var pageNo = $('<input type="text" name="pageNo" />');
		pageNo.attr('maxlength', ('' + self.pageCount).length);
		pageNo.change(function() {
			this.value = (this.value <= self.pageCount) ? this.value
					: self.pageCount;
			this.value = (this.value > 0) ? this.value : 1;
		});
		self.append(pageNo);
		var btnGo = $('<span class="button"></span>').click(function() {
			var pn = pageNo.val();
			if (!pn || isNaN(pn) || pn > self.pageCount)
				pn = 0;
			self.pageNo = pn;
			self.loadPage.call(self, self.pageNo, self.pageSize);
		}).append(Msg.pager.go);
		self.append(btnGo);
	};
	return self;
};
///<jscompress sourcefile="pager2.js" />
/**
 * mac.pager
 * version: 2.0
 * author: Mac_J@boarsoft.com
 * need: core.js
 */
(function($){
mac.pager = function(self, cfg) {
//	cfg.msg = cfg.msg || {
//		page: '{0}, {1}/{2}',
//		go: '跳转',
//		total: '共'
//	};
	self.config = cfg;
	self.update = function(tt, ps, pc, pn) {
		self.empty();
		self.total = tt;
		self.pageCount = pc;
		self.pageNo = pn;
		self.pageSize = ps;
		//var msg = cfg.msg;
        //self.append(mac.getMsg(msg.page, [ tt, pn, pc ]));
        //First
        self.append('<span action="1" class="pageNo">首页</span>');
        //Prev Skip
        var prevskip=pn - cfg.pagerLength;
        if(prevskip>0)
            self.append('<span action="'+prevskip+'" class="pageNo">前'+cfg.pagerLength+'页</span>');
        else
            self.append('<span action="1" class="pageNo">前'+cfg.pagerLength+'页</span>');
        //Prev
        var prev=pn - 1;
        if(prev>0)
            self.append('<span action="'+prev+'" class="pageNo">上一页</span>');
        else
            self.append('<span action="1" class="pageNo">上一页</span>');
        //Next Skip
        var nextskip=pn + cfg.pagerLength;
        if(nextskip<pc)
            self.append('<span action="'+nextskip+'" class="pageNo">后'+cfg.pagerLength+'页</span>');
        else
            self.append('<span action="'+pc+'" class="pageNo">后'+cfg.pagerLength+'页</span>');
        //Next
        var next=pn + 1;
        if(next<pc)
            self.append('<span action="'+next+'" class="pageNo">下一页</span>');
        else
            self.append('<span action="'+pc+'" class="pageNo">下一页</span>');
        //Last
        self.append('<span action="'+pc+'" class="pageNo">尾页</span>');

//		var pl = cfg.pagerLength, hf = Math.floor(pl / 2), hm = pl % 2;
//		var start = Math.max(1, Math.min(pn - hf, pc - pl + 1));
//		var end = Math.min(start + pl, pc + 1);
//		if (start > 1)
//			self.append('<span action="1" class="pageNo">|&lt;&lt;</span>');
//        if (start > 1) {
//            var a = $('<span class="pageNo">前'+cfg.pagerLength+'页</span>');
//            var prevskip=start - cfg.pagerLength;
//            if(prevskip>0)
//                a.attr('action',prevskip);
//            else
//                a.attr('action',1);
//            self.append(a);
//        }
//		if (start > pl) {
//			var a = $('<span class="pageNo">&lt;&lt;</span>');
//			a.attr('action', start - hf + (hm > 0) ? 0 : 1);
//			self.append(a);
//		}
//		for ( var i = start; i < end; i++) {
//			var a = $('<span class="pageNo"></span>');
//			if (i != pn)
//				a.attr('action', i);
//			self.append(a.append(i));
//		}
//		if (end <= pc - pl) {
//			var a = $('<span class="pageNo">&gt;&gt;</span>');
//			a.attr('action', end + hf);
//			self.append(a);
//		}
//        if (end <pc) {
//            var a = $('<span class="pageNo">后'+cfg.pagerLength+'页</span>');
//            var nextskip=start - cfg.pagerLength;
//            if(nextskip>0)
//                a.attr('action',nextskip);
//            else
//                a.attr('action',end);
//            self.append(a);
//        }
//		if (end < pc) {
//			var a = $('<span class="pageNo">&gt;&gt;|</span>');
//			self.append(a.attr('action', pc));
//		}
		self.children('span[action]').click(function() {
			self.pageNo = $(this).attr('action');
			cfg.loadPage.call(self, self.pageNo, self.pageSize);
		});

        self.append('&nbsp;&nbsp;第');
		var pnTf = $('<input type="text" name="pageNo"/>');
		pnTf.attr('maxlength', (''+pc).length);
        pnTf.val(pn);
		pnTf.change(function() {
			this.value = Math.max(1, Math.min(this.value, pc));
		});
        pnTf.keydown(function(e) {
            var key = e.which;
            if (key == 13) {
                e.preventDefault();
                var n = pnTf.val() || 1;
                if (isNaN(n) || n > self.pageCount)
                    n = 1;
                self.pageNo = n;
                cfg.loadPage.call(self, n, self.pageSize);
            }
        });

		self.append(pnTf);
//		$('<span class="button"></span>').click(function() {
//			var n = pnTf.val() || 1;
//			if (isNaN(n) || n > self.pageCount)
//				n = 1;
//			self.pageNo = n;
//			cfg.loadPage.call(self, n, self.pageSize);
//		}).append('跳转').appendTo(self);

//        self.append('&nbsp;&nbsp;当前第'+pn+'页，共'+pc+'页');
        self.append('页，共&nbsp;'+(pc||0)+'&nbsp;页');
	};
	return self;
}
})(jQuery);;
///<jscompress sourcefile="placeholder.js" />
/**
 * Magic placeholder 1.0
 * Author: Mac_J@boarsoft.com
 * Need: core.js
 */
(function($){
mac.placeholder = function(self, cfg) {
	if (window.applicationCache)
		return self; //如果支持HTML5则直接返回
	var dk = self.attr('placeholder');
	self.focusout(function(){
		var el = $(this), v = el.val();
		if ((!v || v == dk) && !el.hasClass('empty')) {
			el.addClass('empty');
			el.val(dk);
		}
	}).focusin(function(){
		var el = $(this), v = el.val();
		el.removeClass('empty').val(v==dk?'':v);
	}).val(dk);
	return self;
}
})(jQuery);;
///<jscompress sourcefile="tabs.js" />
/**
 * Magic tabs plugin 1.3.4
 * Author: Mac_J@boarsoft.com
 * need: core.js
 */
mac.tabs = function(self, cfg) {
	cfg = self.config = $.extend({
		speed : 6,
		tabHeight : 26,
		hbarHeight : 2
	}, cfg);
	var th = cfg.tabHeight, bh = th + cfg.hbarHeight,
		hx = $('<div class="sbtn"><span></span></div>'),
		hm = $('<div class="main"></div>').height(th),
		ht = $('<div class="tt"></div>').height(th);
	hx.width(th).height(th);
	var hb = $('<div class="hbar"></div>');
	hb.height(cfg.hbarHeight).css('top', th);
	var hd = $('<div class="head"></div>').height(th);
	self.append(hb).append(hd);
	var hl = hx.clone().addClass('left'), hr = hx.clone().addClass('right');
	hd.append(hl).append(hm.append(ht)).append(hr);
	var bd = $('<div class="body"></div>').appendTo(self);
	self.adjust = function() {
		var sw = self.width(), sh = self.height(), h = sh - hd.height() - 1;
		bd.height(h);
		bd.children('.main').each(function(n, c) {
			var ec = $(c), x = ec.attr('height');
			if (x) {
				if (x == 'auto') {
					ec.css('height', null);
				} else if (x == '100%') {
					ec.height(h);
				} else {
					ec.css('height', x);
				}
			} else {
				ec.height(bd.height() - hb.height());
			}
		});
		if (hd.width()) {
			var b = hd.width()<=ht.width();
			hl.toggle(b);
			hr.toggle(b);
			hm.width(hd.width() - (b ? hl.width() * 2 + 2 : 0));
		}
		return self;
	}
	function closeTab(a, b, c) {
		if (!a.hasClass('closeable'))
			return;
		c = c || a.attr('name');
		b = b || bd.children('[name=' + c + ']');
		if (cfg.onCloseTab && !cfg.onCloseTab(self, c, a))
			return false;
		var s = a.next('.item');
		if (s.length != 1)
			s = a.prev('.item');
		var t = self.selected, t = (t ? t.attr('name') : '');
		if (c == t && s.length == 1)
			s.click();
		a.hide();
		b.hide();
		window.setTimeout(function() {
			a.remove();
			b.remove();
			if (cfg.onTabClosed)
				cfg.onTabClosed(self, c, a);
		}, 0);
		self.adjust();
	}
	self.closeTab = function(c, a) {
		a = a || hd.seek(c);
		closeTab(a, 0, c);
	}
	self.closeTabs = function(x) {
		hd.find('.item').each(function(n, a) {
			var o = $(a), c = o.attr('name');
			if (c != x)
				closeTab(o, 0, c);
		});
	}
	self.addTab = function(p, n, cb) {
		var k = p.code || 'm' + n;
		var a = hd.seek(k), b;
		if (p.update && a.length > 0) {
			self.select(k);
			b = bd.children('[name=' + k + ']');
			b.empty();
		} else {
			a = $('<div class="item normal" name="' + k + '"></div>');
			b = $('<div class="main hidden" name="' + k + '"></div>');
			if (p.bodyCls)
				b.addClass(p.bodyCls);
			if (p.height)
				b.attr('height', p.height);
			ht.append(a.height(th));
			self.adjust();
			var m = $('<div class="main"></div>');
			m.append(p.title);
			var bw = 0;// xbtn width
			a.append('<div class="left"></div>').append(m);
			if (p.closeable) {
				var x = $('<span class="icon icon-close"></span>');
				var w = $('<div class="xbtn"></div>').append(x);
				x.click(function() {
					closeTab(a, b, k);
					return false;
				});
				a.addClass('closeable').append(w);
				bw = x.width();
			}
			if (cfg.tabWidth)
				m.width(cfg.tabWidth - bw);
			a.append('<div class="right"></div>');
			bd.append(b);
		}
		a.unbind('click');
		a.click(function() {
			var s = self.selected;
			if (s) {
				s.removeClass("selected");
				bd.children('[name=' + s.attr('name') + ']').hide();
			}
			self.selected = a.addClass("selected");
			if (p.url && !b.html() && !p.autoLoad) {
				b.load(p.url, p.params, function() {
					if (cfg.onLoadPage) {
						cfg.onLoadPage(self, a, b, p);
					}
				});
			}
			var h = b.attr('height');
			if (!h) {
				bd.height(self.height() - hd.height() - 4);
			} else if (h == 'auto') {
				bd.css('height', h);
			} else {
				bd.css('overflow', 'hidden');
				bd.height(h);
			}
			b.show();
			if (cfg.onShowTab) {
				cfg.onShowTab(self, a, b, p);
			}
		});
		if (p.el) {
			b.append(p.el);
		} else if (p.render) {
			p.render(b, cfg);
		} else if (p.url && p.autoLoad) {
			b.load(p.url, p.params, function() {
				if (cfg.onLoadPage) {
					cfg.onLoadPage(self, a, b, p);
				}
			});
		}
		if (cb) {
			cb(a, b, p);
		}
		self.adjust();
		self.scroll(a);
		return a;
	}
	$.each(cfg.items, function(n, p) {
		self.addTab(p, n);
	});
	self.hscroll = function() {
		var s = cfg.speed * ($(this).hasClass('left') ? -1 : 1);
		$(document).mouseup(function() {
			var t = self.timer;
			if (t)
				window.clearInterval(t);
		});
		self.timer = window.setInterval(function() {
			var l = hm.scrollLeft();
			hm.scrollLeft(l + s);
		}, 20);
		return self.timer;
	};
	hl.mousedown(self.hscroll);
	hr.mousedown(self.hscroll);
	self.adjust();
	self.scroll = function(a) {
		var al = a.position().left;
		var pl = hm.position().left;
		if (al < pl || al + a.width() > pl + hm.width())
			hm.scrollLeft(hm.scrollLeft() + al - pl);
		return self;
	}
	function select(a, c) {
		a.click();
		self.scroll(a);
		return a;
	}
	self.selectFirst = function() {
		var a = hd.find('.item:first');
		return select(a, a.attr('name'));
	}
	self.select = function(c) {
		return select(hd.seek(c), c);
	}
	self.getTabBody = function(c) {
		return bd.seek(c);
	}
	return self;
};
///<jscompress sourcefile="webapp.js" />
/**
 * Magic webapp.js 1.0.2
 * Author: Mac_J@boarsoft.com
 */
var Cache = {};
var App = {
	schemes: {
		basic: {}
	},
	//a：操作内容， d：返回的消息编码，m：消息模板
	error: function(a, d, m){
		var s = mac.eval('Msg.' + d);
		if (d == 'error.needLogin' || d == 'error.logonTimeout') {
			alert(s);
			window.location.href = App.ctx + '/login.htm';
		} else {
			mac.alert(m || Msg.info.error, [ a, s ]);
		}
	},
	init : function(aa, pg) {
		pg.loading = aa.length;
		mac.loader(pg);
		$.each(aa, function(i, a) {
			$.post(a.url, a.params, function(rs) {
				a.callback(mac.eval(rs));
				pg.loading--;
				if (pg.loading==0 && pg.init) {
					pg.init();
				}
			});
		});
	},
	open: function(c, cb, al) {
		var x = App[c.ctx];
		if (!x) {
			alert('Context ' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var d = x[c.action];
		if (!d) {
			alert('Action ' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var v = App.schemes[App.scheme];
		if (!v) {
			alert('Scheme ' + App.scheme + ' is not exists');
			return;
		}
		v = v[c.ctx];
		if (!v) {
			alert('Scheme ' + App.scheme + '/' + c.ctx + ' is not exists');
			return;
		}
		v = v[c.action];
		if (!v) {
			alert('Scheme ' + App.scheme + '/' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var a = $('<div class="window ' + c.action + '"></div>');
		a.callback = cb;
		a.params = c.params;
		a.mac('dialog', {
			title : c.title || d.title || '',
			width : c.width || v.width,
			height : c.height || v.height,
			minWidth : c.width || v.minWidth,
			minHeight : c.height || v.minHeight,
			position : v.position,
			resizable : v.resizable,
			model : (c.model==undefined? v.model:c.model),
			autoOpen : true,
			onClose : c.onClose,
			noCloser : c.noCloser,
			closeAction: c.closeAction || d.closeAction || 'remove',
			draggable: true,
			closeOnEsc: (c.closeOnEscape==undefined? d.closeOnEscape:c.closeOnEscape)
		});
		d.ui = a;
		if (c.rpath) {
			d.rpath = c.rpath;
			d.ui.load(c.rpath + d.url, null, al);
		} else if (c.el) {
			d.ui.body.append(c.el);
		} else {
			d.rpath = '';
			d.ui.body.load(d.url, null, al);
		}
		return a;
	},
	popup : function(c, cb, al) {
		var x = App[c.ctx];
		if (!x) {
			alert('Context ' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var d = x[c.action];
		if (!d) {
			alert('Action ' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var v = App.schemes[App.scheme];
		if (!v) {
			alert('Scheme ' + App.scheme + ' is not exists');
			return;
		}
		v = v[c.ctx];
		if (!v) {
			alert('Scheme ' + App.scheme + '/' + c.ctx + ' is not exists');
			return;
		}
		v = v[c.action];
		if (!v) {
			alert('Scheme ' + App.scheme + '/' + c.ctx + '/' + c.action + ' is not exists');
			return;
		}
		var a = $('<div class="' + c.action + '"></div>').appendTo($('body'));
		a.callback = cb;
		a.params = c.params;
		a.width(c.width || v.width).height(c.height || v.height).show();
		a.close = a.remove;
		d.ui = a;
		if (c.rpath) {
			d.rpath = c.rpath;
			a.load(c.rpath + d.url, null, al);
		} else if (c.el) {
			a.append(c.el);
		} else {
			d.rpath = '';
			a.load(d.url, null, al);
		}
		return a;
	},
	show : function(pa, uc, pe, al) {
		var pe = pe || App.main.css("height", "")
			, xt = App[pa.ctx];
		if (!xt) {
			alert('Context ' + pa.ctx + '/' + pa.action + ' is not exists');
			return;
		}
		var mo = xt[pa.action];
		if (!mo) {
			alert('Action ' + pa.ctx + '/' + pa.action + ' is not exists');
			return;
		}
		mo.ui = $('<div class="' + (pa.id || pa.action) + '"></div>');
		if (App.timer) {
			window.clearInterval(App.timer);
		}
		pe.empty().append(mo.ui);
		//if (pa.params || mo.params) {
		//	mo.ui.params = $.extend(pa.params || {}, mo.params || {});
		//}
		mo.ui.params = pa.params;
		var w = mac.wait(Msg.loading);
		var uu = mo.ui;
		mo.ui.load(mo.url, function(a) {
			// window.location.hash = pa.action;
			w.close();
			if (pe.afterLoad)
				pe.afterLoad();
			if (al)
				al(mo.ui);
		});
		mo.ui.callback = uc;
		return mo.ui;
	},
	getQueryParam: function(k) {
		var r = new RegExp("(^|&)" + k + "=([^&]*)(&|$)", "i")
			, s = window.location.search.substr(1).match(r);
		return (s != null ? unescape(s[2]): null);
	},
	getHashParam: function(k) {
		var r = new RegExp("(^|&)" + k + "=([^&]*)(&|$)", "i")
			, h = window.location.hash, s = h.substr(h.indexOf('?') + 1).match(r);
		return (s != null ? unescape(s[2]): null);
	},
	websocket: {
		listeners: {},
		open: function(la){
			var me = this, la = la || {};
			if (me.url) {
				$.extend(me.listeners, la);
				return me;
			}
			var wl = window.location,
				url = me.url = 'ws://' + wl.host + wl.pathname + 'websocket',
				ws = me.ws = new WebSocket(url);
			me.listeners = la;
			console.info('Connect websocket ' + url);
			
			ws.onopen = function (e){
				ws.send("{ token: '" + App.logon.token + "', type: 0, group: 'init' }");
				var fa = la.onopen || [];
				for (var i in fa) {
					fa[i](e, ws);
				}
			};
			ws.onclose = function (e){
				var fa = la.onclose || [];
				for (var i in fa) {
					fa[i](e, ws);
				}
				ws = null;
			};
			ws.onerror = function (e) {
				var fa = la.onerror || [];
				for (var i in fa) {
					fa[i](e, ws);
				}
				try {
					ws.close();
				} catch (x) {
					console.error(x);
				} finally {
					ws = null;
					console.error(e);
				}
			};
			ws.onmessage = function (e){
				console.info('Incoming websocket message: ' + e.data);
				try {
					var o = mac.eval(e.data), g = la[o.group];
					if (g) {
						var c = g[o.code];
						for (var k in c) {
							var f = c[k];
							try {
								f(o, e, ws);
							} catch (e) {
								console.error(e);
								console.info('Remove invalid listener '
									+ o.group + '/' + o.code + '/' + k);
								delete c[k];
							}
						}
						return;
					}
					console.warn('Unknown websocket message: ' + e.data);
				} catch (x) {
					console.error(x);
				}
			};
		},
		on: function(g, c, k, f){
			var ga = this.listeners, ca = ga[g];
			if (!ca) {
				ga[g] = ca = {};
			}
			var fa = ca[c];
			if (!fa) {
				ca[c] = fa = {};
			}
			fa[k] = f;
		},
		off: function(g, c, k){
			var ca = this.listeners[g] || {};
			console.info('delete websocket listener: ' + g + '/' + c + '/' + k);
			delete (ca[c] || {})[k];
		}
	}
}
// -------------
Number.prototype.to3ps = function(d) {
	d = d || 0;
	var s = String(this.toFixed(d));
	var g = /(\d{1,3})(?=(\d{3})+$)/g;
	if (d < 1)
		return s.replace(g, "$1,");
	var i = s.length - d;
	var p = s.substr(0, i - 1);
	var x = s.substr(i);
	p = p.replace(g, "$1,");
	return p + '.' + x;
}
Date.prototype.format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
		"H+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	// 毫秒
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}
	if (/(E+)/.test(fmt)) {
		// fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ?
		// (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[this.getDay() +
		// ""]);
		var r = RegExp.$1, w = Msg.datepicker, d = this.getDay();
		fmt = fmt.replace(r, r.length > 1 ? (r.length > 2 ? w.dayNames[d]
				: w.dayNamesShort[d]) : '');
	}
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}
String.prototype.len=function() {
	return this.replace(/[^\x00-\xff]/g,"rr").length;
}
String.prototype.sub = function(n) {
	var r = /[^\x00-\xff]/g;
	if (this.replace(r, "mm").length <= n)
		return this;
	var m = Math.floor(n / 2);
	for (var i = m; i < this.length; i++) {
		if (this.substr(0, i).replace(r, "mm").length >= n) {
			return this.substr(0, i);
		}
	}
	return this;
}
String.prototype.gblen = function() {
	var len = 0;
	for (var i = 0; i < this.length; i++) {
		if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
			len += 2;
		} else {
			len++;
		}
	}
	return len;
};
///<jscompress sourcefile="window.js" />
/**
 * Magic window pluing 1.0
 * Author: Mac_J@boarsoft.com
 * Need: core.js
 */
mac.window = function(self, cfg) {
	// setting tabIndex makes the div focusable
	// setting outline to 0 prevents a border on focus in Mozilla
	self.attr('tabIndex', -1).css('outline', 0);
	self.mousedown(function(e) {
		self.move2top();
	});
	var bd = self.children('.body');
	var hd = self.children('.head');
	hd.find('.icon').hover(function() {
		$(this).addClass('ui-state-hover');
	}, function() {
		$(this).removeClass('ui-state-hover');
	});
	hd.find('.closer').click(function(e) {
		if(cfg.closeAction=='remove')
			self.remove();
		else
			self.hide();
		return false;
	});
	hd.find('.pin').click(function(e) {
		self.pined = !self.pined;
		var c = 'ui-icon-pin-';
		var x = 's', y = 'w';
		if (self.pined) {
			x = 'w';
			y = 's';
		}
		$(this).removeClass(c + x).addClass(c + y);
		return false;
	});
	self.move2top = function() {
		var z = $(this).css('z-index');
		if (z != 'auto' && z >= $.ui.dialog.maxZ) {
			$.ui.dialog.maxZ = z;
		} else {
			$(this).css('z-index', ++$.ui.dialog.maxZ);
		}
		return self;
	}
	if (cfg.draggable) {
		self.draggable({
			cancel : '.body, .closer',
			handle : '.head',
			// containment: self.parent(),
			start : function() {
				if (self.pined)
					return false;
				if (cfg.onStartDrag)
					cfg.onStartDrag();
				return true;
			},
			drag : cfg.onDrag,
			stop : cfg.onStopDrag
		});
	}
	if (cfg.resizable) {
		// .ui-resizable has position: relative defined in the stylesheet
		// but dialogs have to use absolute or fixed positioning
		var p = self.css('position');
		self.resizable({
			cancel : '.body',
			// containment: self.parent(),
			// alsoResize: self.find('.body'),
			maxWidth : cfg.maxWidth,
			maxHeight : cfg.maxHeight,
			minWidth : cfg.minWidth,
			minHeight : cfg.minHeight,
			handles : cfg.handles,
			start : cfg.onStartResize,
			resize : function(){
				var h = self.height()-hd.height()-4;
				bd.height(h);
				if(cfg.onResize)
					cfg.onResize(bd.width(), h);
			},
			stop : cfg.onStopResize
		});
		self.css('position', p);
	}
	bd.height(self.height()-hd.height()-4);
	return self;
};
